package com.servicesystem.servicesystem.controller.ali;

import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/auth")
public class AuthController {

    private final static String GET_CODE = "https://auth.1688.com/oauth/authorize?client_id=5307039&site=1688&redirect_uri=http://localhost:10001/auth/token-get&state=http://localhost:10001/1688/account";
    private static String GET_TOKEN = "https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/5307039?grant_type=authorization_code&need_refresh_token=true&client_id=5307039&client_secret=QZtbsGayW7";

    @GetMapping("/code-get")
    public ModelAndView getCode() {


        return new ModelAndView("redirect:" + GET_CODE);
    }

    @GetMapping("/token-get")
    public R getAliToken(String code, String state) {
        GET_TOKEN = GET_TOKEN + "&redirect_uri=" + state + "&code=" + code;
        HttpRequestAll httpRequestAll = new HttpRequestAll();
        R result;
        try {
            result = httpRequestAll.doPost(GET_TOKEN);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("授权失败");
    }
}
